package com.jason.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.jason.system.common.ResultEntity;
import com.jason.system.common.util.Pager;
import com.jason.system.entity.AdminPermission;
import com.jason.system.entity.AdminRole;
import com.jason.system.service.PermissionService;
import com.jason.system.service.RoleService;
import io.swagger.annotations.*;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author jie
 * @version 1.0
 * success come from self-discipline
 * @date 2021/4/1 21:28
 */
@RestController
@RequestMapping("/api/permission")
@Api(value = "/permission",tags = "权限接口")
public class PermissionController {

  private static final Logger logger = LoggerFactory.getLogger(PermissionController.class);

  @Autowired
  PermissionService permissionService;

  @Autowired
  RoleService roleService;

  @ApiOperation(value = "权限获取",notes = "获取所有权限信息")

  @RequiresRoles("role:root")
  @GetMapping("/getAll")
  public JSONObject getAll(){
    logger.info("==>>>PermissionController==>>>进行路径：/api/permission/getAll.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    List<AdminPermission> permissionList = permissionService.getAll();
    logger.info("==>>>获取所有权限信息==>>>: {}",permissionList);
    if (permissionList!=null) {
      r.setCode(200);
      r.setMessage("获取所有权限集合成功");
      r.setData(permissionList);
    }else {
      r.setCode(201);
      r.setMessage("获取所有权限集合失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }


  @ApiOperation(value = "角色-权限",notes = "查询角色权限的关联")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "query",name = "page", value = "页数",required = true,dataType = "Integer"),
          @ApiImplicitParam(paramType = "query",name = "size",value = "没页个数",required = true,dataType = "Integer")
  })
  @ApiResponses({
          @ApiResponse(code=200,message = "获取角色和权限的关联成功"),
          @ApiResponse(code = 201,message = "获取角色和权限的关联失败")
  })
  @RequiresRoles("role:root")
  @PostMapping("/getRolePermission")
  public JSONObject getAllRolePermission(@RequestParam("page") Integer page, @Param("size") Integer size){
    logger.info("==>>>PermissionController==>>>进行路径：/api/permission/getRolePermission.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>PermissionController==>>>获取角色和权限的关系中...");
    Pager<AdminRole> roleList = roleService.getAllRolePermission(page, size);
    logger.info("==>>>PermissionController==>>>获取角色和权限的关系完成...");
    if(roleList!=null){
      r.setCode(200);
      r.setMessage("获取角色和权限的关联成功");
      r.setData(roleList);
    }else {
      r.setCode(201);
      r.setMessage("获取角色和权限关联失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value ="分配权限",notes = "为角色分配权限")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "query",name = "rId",value = "角色id",required = true,dataType = "Integer"),
          @ApiImplicitParam(paramType = "query",name = "permissions",value ="权限id的字符串集合，以逗号分割",required = true,dataType = "String")
  })
  @ApiResponses({
          @ApiResponse(code = 200,message ="角色分配权限成功"),
          @ApiResponse(code = 201,message ="角色分配权限失败")
  })
  @RequiresRoles("role:root")
  @PostMapping("/insertRolePermission")
  public JSONObject insertRolePermission(@RequestParam("rId") Integer rId, @RequestParam("permissions") String permissions){
    logger.info("==>>>PermissionController==>>>进行路径：/api/permission/insertRolePermission.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>PermissionController==>>>添加角色和权限的关系中...");
    boolean result = permissionService.insertToRolePermission(rId,permissions);
    logger.info("==>>>PermissionController==>>>添加角色和权限的关系完成...");
    if(result) {
      r.setCode(200);
      r.setMessage("角色分配权限关系成功");
    }else {
      r.setCode(201);
      r.setMessage("角色分配权限关系失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value = "删除角色权限",notes = "删除角色相关权限")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "query",name = "rId",value = "角色id",required = true,dataType = "Integer"),
          @ApiImplicitParam(paramType = "query",name = "permissions",value ="权限id的字符串集合，以逗号分割",required = true,dataType = "String")
  })
  @ApiResponses({
          @ApiResponse(code = 200,message ="删除角色所属权限成功"),
          @ApiResponse(code = 201,message ="删除角色所属权限失败")
  })
  @RequiresRoles("role:root")
  @PostMapping("/deleteRolePermission")
  public JSONObject deleteRolePermission(@RequestParam("rId") Integer rId, @RequestParam("permissions") String permissions){
    logger.info("==>>>PermissionController==>>>进行路径：/api/permission/deleteRolePermission.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>PermissionController==>>>删除角色和权限的关系中...");
    boolean result = permissionService.deleteToRolePermission(rId,permissions);
    logger.info("==>>>PermissionController==>>>删除角色和权限的关系完成...");
    if (result){
      r.setCode(200);
      r.setMessage("删除角色和权限关系成功");
    }else {
      r.setCode(201);
      r.setMessage("删除角色和权限关系失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

}
